<?
date_default_timezone_set('UTC');

define("MAJOR_VER", 1);
define("MINOR_VER", 0);

function checkParam($param) {
	if (!isset($param) || empty($param))
		return false;
	else
		return true;
}

function handlePostRequest($type) {
	if (!checkParam($type)) {
		echo "Invalid type";
		return;
	}
	
	
	
	switch ($type) {
		case 'setUserExchangeInfo':
			{
				if (!checkParam($_POST['uid']) || !checkParam($_POST['pid'])) {
					echo "parameter failed";
					return;
				}
				
				$pdo = new PDO("sqlite:UserInfo.sqlite");
				// 準備新增資料的語法
				$sql = "INSERT INTO 'UserExchgInfo' ('Uid', 'ProductId', 'ExchangeTimestamp') VALUES(:uid, :pid, :extime)";
				$pdoStatement = $pdo->prepare($sql);
				
				if(!$pdoStatement) {
					print_r($pdo->errorInfo());
					exit();
				}

				// 塞入資料並執行
				$count = $pdoStatement->execute(
				        array(
				                ':uid' => $_POST['uid'],
				                ':pid' => $_POST['pid'],
				                ':extime' => date('U')
				        )
				);
				 
				$sid = $pdo->lastInsertId();
				echo 'lastInsertId:' . $sid;
				
			}
			break;
		case 'setUserInfo':
			{
				if (!checkParam($_POST['uid'])) {
					echo "parameter failed";
					return;
				}
				//if (!is_numeric($_POST['pid'])) {
				//	echo "pid type failed";
				//	return;
				//}
				$pdo = new PDO("sqlite:UserInfo.sqlite");
				// update data
				$updateSql = "UPDATE 'UserData' SET Name=:name, Money=:money, AngelCash=:angelCash, Energy=:energy,
						Mood=:mood, Health=:health, Elegant=:elegant, Energetic=:energetic, Romantic=:romantic,
						Virtuous=:virtuous, Sexy=:sexy, AngelPoint=:angelPoint, ItemList=:itemList, EvtCntList=:evtCntList,
						RunningSecs=:runningSecs, ForegroundSecs=:foregroundSecs, AngelBarClkCnt=:angelBarClkCnt,
						MaxAngelBarClkCnt=:maxAngelBarClkCnt, EvtCancelCnt=:evtCancelCnt, RejectIgnoreInviteCnt=:rejectIgnoreInviteCnt,
						EarnMoney=:earnMoney, EarnCash=:earnCash, LastLoginTime=:lastLoginTime, LastLoginTimeStr=:lastLoginTimeStr
						WHERE Uid=:uid";
				$pdoStatement = $pdo->prepare($updateSql);
				if(!$pdoStatement) {
					print_r($pdo->errorInfo());
					exit();
				}
				// 塞入資料並執行
				$pdoStatement->execute(
				        array(
				                ':uid' => $_POST['uid'],
				                ':name' => $_POST['name'],
				                ':money' => $_POST['money'],
				                ':angelCash' => $_POST['angelCash'],
				                ':energy' => $_POST['energy'],
				                ':mood' => $_POST['mood'],
				                ':health' => $_POST['health'],
				                ':elegant' => $_POST['elegant'],
				                ':energetic' => $_POST['energetic'],
				                ':romantic' => $_POST['romantic'],
				                ':virtuous' => $_POST['virtuous'],
				                ':sexy' => $_POST['sexy'],
				                ':angelPoint' => $_POST['angelPoint'],
				                ':itemList' => $_POST['itemList'],
				                ':evtCntList' => $_POST['evtCntList'],
				                ':runningSecs' => $_POST['runningSecs'],
				                ':foregroundSecs' => $_POST['foregroundSecs'],
				                ':angelBarClkCnt' => $_POST['angelBarClkCnt'],
				                ':maxAngelBarClkCnt' => $_POST['maxAngelBarClkCnt'],
				                ':evtCancelCnt' => $_POST['evtCancelCnt'],
				                ':rejectIgnoreInviteCnt' => $_POST['rejectIgnoreInviteCnt'],
				                ':earnMoney' => $_POST['earnMoney'],
				                ':earnCash' => $_POST['earnCash'],
				                ':lastLoginTime' => date('U'),
				                ':lastLoginTimeStr' => date('Y-m-d G:i:s')
				        )
				);
				$updateCount = $pdoStatement->rowCount();
				if ($updateCount == 0) {
					$insertSql = "INSERT INTO 'UserData' ('Uid', 'Name', 'Money', 'AngelCash', 'Energy', 'Mood', 'Health',
									'Elegant', 'Energetic', 'Romantic', 'Virtuous', 'Sexy', 'AngelPoint', 'ItemList', 'EvtCntList',
									'RunningSecs', 'ForegroundSecs', 'AngelBarClkCnt', 'MaxAngelBarClkCnt', 'EvtCancelCnt',
									'RejectIgnoreInviteCnt', 'EarnMoney', 'EarnCash', 'LastLoginTime', 'LastLoginTimeStr', 'CreateTimestamp') 
									VALUES (:uid, :name, :money, :angelCash, :energy, :mood, :health, :elegant, :energetic,
									:romantic, :virtuous, :sexy, :angelPoint, :itemList, :evtCntList, :runningSecs, :foregroundSecs,
									:angelBarClkCnt, :maxAngelBarClkCnt, :evtCancelCnt, :rejectIgnoreInviteCnt, :earnMoney, :earnCash,
									:lastLoginTime, :lastLoginTimeStr, :createTimestamp)";
					$pdoStatement = $pdo->prepare($insertSql);
					if(!$pdoStatement) {
						print_r($pdo->errorInfo());
						exit();
					}
					//$pdoStatement->execute();
					// 塞入資料並執行
					$pdoStatement->execute(
					        array(
					                ':uid' => $_POST['uid'],
					                ':name' => $_POST['name'],
					                ':money' => $_POST['money'],
					                ':angelCash' => $_POST['angelCash'],
					                ':energy' => $_POST['energy'],
					                ':mood' => $_POST['mood'],
					                ':health' => $_POST['health'],
					                ':elegant' => $_POST['elegant'],
					                ':energetic' => $_POST['energetic'],
					                ':romantic' => $_POST['romantic'],
					                ':virtuous' => $_POST['virtuous'],
					                ':sexy' => $_POST['sexy'],
					                ':angelPoint' => $_POST['angelPoint'],
					                ':itemList' => $_POST['itemList'],
					                ':evtCntList' => $_POST['evtCntList'],
					                ':runningSecs' => $_POST['runningSecs'],
					                ':foregroundSecs' => $_POST['foregroundSecs'],
					                ':angelBarClkCnt' => $_POST['angelBarClkCnt'],
					                ':maxAngelBarClkCnt' => $_POST['maxAngelBarClkCnt'],
					                ':evtCancelCnt' => $_POST['evtCancelCnt'],
					                ':rejectIgnoreInviteCnt' => $_POST['rejectIgnoreInviteCnt'],
					                ':earnMoney' => $_POST['earnMoney'],
					                ':earnCash' => $_POST['earnCash'],
					                ':lastLoginTime' => date('U'),
					                ':lastLoginTimeStr' => date('Y-m-d G:i:s'),
					                ':createTimestamp' => date('U')
					        )
					);
					$sid = $pdo->lastInsertId();
					//print_r($pdo->errorInfo());
					//$insertCount = $pdoStatement->rowCount();
					echo 'Insert id: ' . $sid;
				} else {
					echo 'Update Count: ' . $updateCount;
				}
			}
			break;
	}
}

function handleGetRequest($type) {
	switch ($type) {
		case 'getDateTime':
			{
				//date_default_timezone_set('UTC');
				echo date('U');
			}
			break;
		case 'getMallData':
			{
				$pdo = new PDO("sqlite:RIServer.sqlite");
				$pdoStatement = $pdo->query('SELECT * FROM MallItemList');
				echo json_encode( $pdoStatement->fetchAll(PDO::FETCH_ASSOC) );
			}
			break;
		case 'getProductInfo':
			{
				$pdo = new PDO("sqlite:RIServer.sqlite");
				$pdoStatement = $pdo->query('SELECT * FROM ProductInfo');

				echo json_encode( $pdoStatement->fetchAll(PDO::FETCH_ASSOC) );
			}
			break;
		case 'getUserInfo':
			{
				$pdo = new PDO("sqlite:UserInfo.sqlite");
				$pdoStatement = $pdo->query('SELECT * FROM UserData');

				echo json_encode( $pdoStatement->fetchAll(PDO::FETCH_ASSOC) );
			}
			break;
		case 'getUserExchangeInfo':
			{
				$pdo = new PDO("sqlite:UserInfo.sqlite");
				$pdoStatement = $pdo->query('SELECT * FROM UserExchgInfo');

				echo json_encode( $pdoStatement->fetchAll(PDO::FETCH_ASSOC) );
			}
			break;
		case 'getVersion':
			{
				echo MAJOR_VER . ":" . MINOR_VER;
			}
			break;
	}
}


if (isset($_POST["type"]) && checkParam($_POST['type'])) {
	handlePostRequest($_POST['type']);
} else if (isset($_GET["type"]) && checkParam($_GET['type'])) {
	handleGetRequest($_GET['type']);
}


/*if (isset($_POST['setUserExchangeInfo'])) {
	if (!isset($_POST['uid'])) {
		echo "parameter failed";
		return;
	}


	$pdo = new PDO("sqlite:Server.sqlite");

 

	// 準備新增資料的語法
	
	$sql = "INSERT INTO 'UserInfo' ('Uid', 'Money', 'AngelCash' )
	
	                VALUES( :uid, :money, :angelCash)";
	
	$pdoStatement = $pdo->prepare($sql);
	
	 
	
	// 塞入資料並執行
	$count = $pdoStatement->execute(
	        array(
	                ':uid' => $_POST['uid'],
	                ':money' => $_POST['money'],
	                ':angelCash' => $_POST['angelCash']
	        )
	);
	
	 
	//echo 'Insert Count: ' . $count . '<br>';
	$sid = $pdo->lastInsertId();
	echo 'lastInsertId: ' . $sid . '<br>';
	
	 
	
	
	
	//$pdoStatement = $pdo->prepare('SELECT * FROM ItemList');
	
	//$pdoStatement->execute();
	
	 
	
	//while($row = $pdoStatement->fetch(PDO::FETCH_ASSOC) ) {
	
	        //print_r( $row );
	        //echo array2json($row);
	
	//}
	//$row = $pdoStatement->fetch(PDO::FETCH_ASSOC);
	//echo json_encode($row);
	
	//echo '------<br>';
	
	
	 
	
	//$pdoStatement = $pdo->query('SELECT * FROM MallItemList');
	
	//echo json_encode( $pdoStatement->fetchAll(PDO::FETCH_ASSOC) );
}*/


?>
